package main.singleton;

/**
 * @Author: SerMs
 * @Date: 2024/06/17 17 23
 * @Email: 1839928782@qq.com
 * @Blog: https://serms.top
 * 使用同步代码块进行双重检查
 **/
public class Singleton06 {
    private static Singleton06 INSTANCE;

    private Singleton06() {
    }

    private static Singleton06 getInstance() {
        // 双重检查 懒汉模式
        if (INSTANCE == null) {
            synchronized (Singleton06.class) {
                if (INSTANCE == null) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    INSTANCE = new Singleton06();
                }

            }
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            // 多线程
            new Thread(() -> System.out.println(Singleton06.getInstance().hashCode())).start();
        }
    }
}
